package jeye.lcdui;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.TextField;

import jeye.lcdui.util.ScreenFactorySupport;
import jeye.midlet.ApplicationController;
import jeye.midlet.ScreenType;

public class CameraPreviewProperties extends Form implements CommandListener, ScreenFactorySupport {
	
	private CameraPreview cameraPreview;
	
	private ApplicationController controller;
	
	private Command applyCommand;
	
	private Command cancelCommand;
	
	private TextField framerateField;
	
	private TextField qualityField;

	public CameraPreviewProperties() {
		super("Opcje podgladu");
	}
	
	public void initialize() {
		applyCommand = new Command("Zastosuj", Command.ITEM, 1);
		cancelCommand = new Command("Anuluj", Command.ITEM, 1);

		addCommand(applyCommand);
		addCommand(cancelCommand);
		
		cameraPreview = (CameraPreview)controller.getApplicationScreen(ScreenType.PREVIEW);
		
		int frameSkip = cameraPreview.getFrameSkip();
		framerateField = new TextField("Frameskip", String.valueOf(frameSkip), 5, TextField.NUMERIC);
		framerateField.setLayout(Item.LAYOUT_EXPAND);
		
		Float quality = cameraPreview.getQuality();
		String qualityStr = (quality == null) ? "" : String.valueOf(quality);
		
		qualityField = new TextField("Jakosc", String.valueOf(qualityStr), 5, TextField.DECIMAL);
		qualityField.setLayout(Item.LAYOUT_EXPAND);
		
		append(framerateField);
		append(qualityField);
		
		setCommandListener(this);
	}

	public void commandAction(Command command, Displayable displayable) {
		if(command.equals(applyCommand)) {
			String qualityStr = qualityField.getString();
			Float quality = null;
			
			if(!qualityStr.equals("")) {
				quality = Float.valueOf(qualityStr);
			}
			
			cameraPreview.setFrameSkip(Integer.valueOf(framerateField.getString()).intValue());
			cameraPreview.setQuality(quality);
		}
		controller.setCurrentDisplay(ScreenType.MAIN_MENU);
	}

	public void setController(ApplicationController controller) {
		this.controller = controller;
	}
}
